home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1034 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.7 KB

  1. From: Nico Josuttis <nico@bredex.de>
  2. Message-ID: <199604110827.KAA02243@bredex.bredex.de>
  3. X-Original-Date: Thu, 11 Apr 96 10:27:06 +0200
  4. Path: in2.uu.net!bounce-back
  5. Date: 11 Apr 96 14:18:37 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Organization: -
  8. Newsgroups: comp.std.c++
  9. X-Authentication-Warning: bredex.bredex.de: Host localhost didn't use HELO protocol
  10. Cc: nico@bredex.de
  11. Subject: STL (specification) BUG: lost function object state in remove_if()
  12. X-Mts: smtp
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBFAgUBMW0U1+EDnX0m9pzZAQHHYgGAl3lgUKMY8qzpXoiGLKuRvGs1+nwGVs1C
  15.     YLzmvxHyII5gWL4zyB4q1Pfsbra7LNWu
  16.     =PtAl
  17.  
  18. Hi,
  19. the big advantage of the usage of function objects with algorithms is,
  20. that they have a state. But there is a problem, this state may get lost
  21. IN THE MIDDLE of a running algorithm like remove_if().
  22.  
  23. This is due to the fact that the implementation calls two function:
  24.  > template <class ForwardIterator, class Predicate>
  25.  > ForwardIterator remove_if(ForwardIterator first, ForwardIterator last,
  26.  >                           Predicate pred) {
  27.  >   first = find_if(first, last, pred);
  28.  >   ForwardIterator next = first;
  29.  >   return first == last ? first : remove_copy_if(++next, last, first, pred);
  30.  > }
  31. But both, find_if() and remove_copy_if(), are called with call-by-value for
  32. pred. This is a problem if for example i want to delete the first occurence
  33. of a value. It results in removing the first and the second value, the first,
  34. because find_if() finds it, the second because remove_copy_if() finds that
  35. as first. Its pred doesn't know that find_if()s-pred has the state changed
  36. into "found".
  37. Tricky problem, isn't it ?
  38.  
  39. I think, we have to change algorithms here so that all internal calls
  40. a made with call by reference.
  41. If this results in prefomance penalties, we should implement the algorithm
  42. in another way.
  43.  
  44. Anyway a big hint should be made in the specification either that this danger
  45. exists or, if it is fixed, that the function object is the original object
  46. operator() is called for every elem.
  47.  
  48. Any meanings ?
  49. --------
  50. Nico                             address: BREDEX GmbH
  51. email:   nico@bredex.de                   Nicolai Josuttis
  52.                                           Fallersleber-Tor-Wall 23
  53. phone:   +49 531 24330-0                  D-38100 Braunschweig
  54. fax:     +49 531 24330-99                 Germany
  55. --------
  56. ---
  57. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  58. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  59. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  60. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  61. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  62.